﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;


namespace Ession.OCR
{
    public class OcrEssion
    {
        public OcrEssion()
        {
            tessocr = new tessnet2.Tesseract();
            tessocr.Init(null, "eng", false);
        }

        private ImageProcessor p;

        public ImageProcessor P
        {
            get { return p; }
            set { p = value; }
        }
        public OcrEssion(string whiteList = "0123456789", string lang = "eng", ImageProcessor p = null)
        {
            this.P = p;
            tessocr = new tessnet2.Tesseract();
            tessocr.SetVariable("tessedit_char_whitelist", whiteList);
            tessocr.Init(null, lang, false);
        }

        private tessnet2.Tesseract tessocr;
        public string GetText(System.Drawing.Image img)
        {
            if (img is Bitmap)
            {

            }
            else
            {
                img = new Bitmap(img);
            }
            if (p != null)
            {
                P.ProcessImage(img as Bitmap);
            }

            List<tessnet2.Word> result = tessocr.DoOCR((Bitmap)img, Rectangle.Empty);
            foreach (tessnet2.Word word in result)
                //Console.WriteLine("{0} : {1}", word.Confidence, word.Text);
                return word.Text;
            return "";
        }
    }

    public class Ocr
    {
        public void DumpResult(List<tessnet2.Word> result)
        {
            foreach (tessnet2.Word word in result)
                //Console.WriteLine("{0} : {1}", word.Confidence, word.Text);
                MessageBox.Show(string.Format("{0} : {1}", word.Confidence, word.Text));
        }

        public List<tessnet2.Word> DoOCRNormal(Bitmap image, string lang)
        {
            tessnet2.Tesseract ocr = new tessnet2.Tesseract();
            ocr.Init(null, lang, false);
            List<tessnet2.Word> result = ocr.DoOCR(image, Rectangle.Empty);
            DumpResult(result);
            return result;
        }

        System.Threading.ManualResetEvent m_event;

        public void DoOCRMultiThred(Bitmap image, string lang)
        {
            tessnet2.Tesseract ocr = new tessnet2.Tesseract();
            ocr.Init(null, lang, false);
            // If the OcrDone delegate is not null then this'll be the multithreaded version
            ocr.OcrDone = new tessnet2.Tesseract.OcrDoneHandler(Finished);
            // For event to work, must use the multithreaded version
            ocr.ProgressEvent += new tessnet2.Tesseract.ProgressHandler(ocr_ProgressEvent);
            m_event = new ManualResetEvent(false);
            ocr.DoOCR(image, Rectangle.Empty);
            // Wait here it's finished
            m_event.WaitOne();
        }

        public void Finished(List<tessnet2.Word> result)
        {
            DumpResult(result);
            m_event.Set();
        }

        void ocr_ProgressEvent(int percent)
        {
            Console.WriteLine("{0}% progression", percent);
        }
    }

}
